package net.j4love.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author He Peng
 * @create 2017-08-05 23:11
 * @update 2017-08-05 23:11
 * @updatedesc : 更新说明
 * @see
 */
public class EchoServerHandler implements Runnable {

    private Socket socket;

    public EchoServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufReader = new BufferedReader(
                    new InputStreamReader(inputStream, "UTF-8"));
            String line = bufReader.readLine();
            String body = line == null ? "" : line;
            System.out.println("Received client data : " + body);
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream);
            writer.write(body);
            writer.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
